<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_custom_model\controller; //Demo插件英文名，改成你的插件英文就行了

use cmf\controller\PluginAdminBaseController;
use plugins\qbt_custom_model\service\FieldService;
use think\Db;



class AdminModelFieldController extends PluginAdminBaseController
{
    private $path;
    private $request_param;
    public function _initialize(){
        $this->path = str_replace("\\",DS,dirname(__DIR__)).DS.'data'.DS;
        $this->request_param = $this->request->param();
        unset($this->request_param['_plugin']);
        unset($this->request_param['_controller']);
        unset($this->request_param['_action']);
    }

    /**
     * 字段管理
     * @adminMenu(
     *     'name'   => '字段管理',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '字段管理',
     *     'param'  => ''
     * )
     */
    public function lists(){
        $model_id = $this->request->param('model_id',0,'intval');
        //取出下面所有的字段信息
        $model_info = Db::name("model")->find($model_id);
        $field_info = Db::name("model_field")->where(['model_id'=>$model_info['model_id']])->order("field_sort asc,field_id asc")->select()->toArray();
//        $page = $field_info->render();
        $formtype = include $this->path."fields.php";

//        $this->assign('page', $page);
        $this->assign("list",$field_info);
        $this->assign("model_info",$model_info);
        $this->assign("formtype",$formtype);
        return $this->fetch();
    }

    /**
     * 添加字段
     * @adminMenu(
     *     'name'   => '添加字段',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加字段',
     *     'param'  => ''
     * )
     */
    public function add(){
        $model_id = $this->request->param('model_id',0,'intval');
        $formtype = include $this->path."fields.php";
        $model_info = Db::name("model")->find($model_id);
        $this->assign('model_info',$model_info);
        $this->assign('formtype',$formtype);
        return $this->fetch();
    }

    /**
     * 添加字段表单处理
     * @adminMenu(
     *     'name'   => '添加字段表单处理',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加字段表单处理',
     *     'param'  => ''
     * )
     */
    public function addPost(){
        $data = $this->request_param;
        $result = $this->validate($data, "AdminModelField");
        if ($result !== true) {
            $this->error($result);
        }
        $model = Db::name("model")->find($data['model_id']);
        //修改对应表
        FieldService::AddField($data,$model['model_tabname']);
        
        //添加进字段
        $model_field = Db::name("model_field")->where('model_id',$data['model_id'])->order('field_sort desc')->find();
        if($model_field){
            $data['field_sort']=$model_field['field_sort']+10;
        }else{
            $data['field_sort']=10;
        }
        Db::name("model_field")->insert($data);
        $this->success("添加字段成功");
    }

    /**
     * 修改字段
     * @adminMenu(
     *     'name'   => '修改字段',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改字段',
     *     'param'  => ''
     * )
     */
    public function edit(){
        $field_id = $this->request->param('field_id',0,'intval');
        $field_info = Db::name("model_field")->find($field_id);
        $formtype = include $this->path."fields.php";
        $model_info = Db::name("model")->find($field_info['model_id']);
        $this->assign('model_info',$model_info);
        $this->assign('field_info',$field_info);
        $this->assign('formtype',$formtype);
        return $this->fetch();
    }

    /**
     * 编辑字段表单处理
     * @adminMenu(
     *     'name'   => '编辑字段表单处理',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑字段表单处理',
     *     'param'  => ''
     * )
     */
    public function editPost(){
        $data = $this->request_param;
        $result = $this->validate($data, "AdminModelField");
        if ($result !== true) {
            $this->error($result);
        }

        $model = Db::name("model")->find($data['model_id']);
        //修改对应表
        FieldService::EditField($data,$model['model_tabname'],2,$data['oldname']);
        //修改字段
        unset($data['oldname']);
        Db::name("model_field")->where("field_id",$data['field_id'])->update($data);

        $this->success("编辑字段成功");
    }

    /**
     * 排序
     * @adminMenu(
     *     'name'   => '排序',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '排序',
     *     'param'  => ''
     * )
     */
    public function list_order(){
        $data = $this->request_param;
        $fieldModel = Db::name("model_field");
        if(isset($data['list_orders'])){
            foreach ($data['list_orders'] as $k=>$v){
                $fieldModel->where(['field_id' => $k])->update(['field_sort' => $v]);
            }
        }
        $this->success("排序更新成功！", '');
    }

    /**
     * 删除字段
     * @adminMenu(
     *     'name'   => '删除字段',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除字段',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $field_id = $this->request->param('field_id',0,'intval');
        $field_info = Db::name("model_field")->find($field_id);
        $model_info = Db::name("model")->find($field_info['model_id']);
        //删除对应表的字段
        $sql = "alter table ".config("database.prefix").$model_info['model_tabname']." drop column ".$field_info['field_name'];
        Db::query($sql);
        //开始删除字段表数据
        Db::name("model_field")->delete($field_id);
        $this->success('删除成功！');
    }
    /**
     * 获取表
     * @adminMenu(
     *     'name'   => '获取表',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '获取表',
     *     'param'  => ''
     * )
     */
    public function getTable(){
        $sql = "show tables";
        $result = Db::query($sql);
        $table_arr = array_column($result,'Tables_in_'.config("database.database"));
        $this->assign('table_arr',$table_arr);
        return $this->fetch();
    }
    /**
     * 获取字段
     * @adminMenu(
     *     'name'   => '获取字段',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '获取字段',
     *     'param'  => ''
     * )
     */
    public function get_field(){
        $tablename = $this->request->param("tablename","","trim");
        //通过表名获取字段
        $sql = "select COLUMN_NAME from information_schema.COLUMNS where table_name = '".$tablename."';";
        $result = Db::query($sql);
        $field_arr = array_column($result,'COLUMN_NAME');
        echo json_encode($field_arr);
    }

}
